<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片式 -->
    <el-card>
      <!-- 检索 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <div>
            <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable class="input-with-select">
              <el-button slot="append" icon="el-icon-search" @click="searchGoods"></el-button>
            </el-input>
          </div>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addGoods" style="float: left">添加商品</el-button>
        </el-col>
      </el-row>
      

      <!-- 添加商品按钮 -->

      <!-- 表格 -->
      <el-table :data="goodsDate" border stripe style="width: 100%; margin-top: 15px">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="640px" style="font-size: 18px"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="110px"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="80px"></el-table-column>
        <el-table-column prop="add_time" label="创建时间">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="130px">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="goodsEdit(scope.row)"></el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="goodsDel(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页器 -->
      <div class="block" style="margin-top: 15px">
        <el-pagination @size-change="sizeChange" @current-change="currentChange" 
        :current-page="queryInfo.pagenum" :page-sizes="[1, 2, 5, 10]" :page-size="queryInfo.pagesize" 
        layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
      </div>
    </el-card>

    <!-- 编辑对话框 -->
    <el-dialog title="编辑商品" :visible.sync="editDialogVisible" width="50%" :before-close="editDialogClose">
      <!-- body -->
      <el-form :model="goodsForm"  :rules="goodsFormRules" ref="goodsFormRef" 
      label-width="80px" class="demo-goodsForm" >
        <el-form-item label="商品名称	" prop="goods_name">
          <el-input v-model="goodsForm.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="分类列表" prop="goods_cat">
          <div class="block">
            <el-cascader expand-trigger="hover" :options="goodsCateList" placeholder="选择商品分类"
            v-model="selectKeys"  @change="selectGoodsChange" :props="goodsCateProps">
            </el-cascader>
          </div>
        </el-form-item>
        <el-form-item label="商品价格" prop="goods_price">
          <el-input v-model.number="goodsForm.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="商品数量" prop="goods_number">
          <el-input v-model.number="goodsForm.goods_number"></el-input>
        </el-form-item>
        <el-form-item label="商品重量" prop="goods_weight">
          <el-input v-model.number="goodsForm.goods_weight"></el-input>
        </el-form-item>
      </el-form>

      <!-- footer -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogClose">取 消</el-button>
        <el-button type="primary" @click="editDialogSure">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>
<script>
export default {
  data() {
    return {
      goodsDate: [],
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      editDialogVisible: false,
      goodsForm: {
        goods_name: '',
        goods_cat: '',
        goods_price: '',
        goods_number: '',
        goods_weight: '',
      },
      goodsFormRules: {
        goods_name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' },
          { min: 1, max: 150, message: '商品名称长度在 1 到 150 个字符', trigger: 'blur' }
        ],
        goods_price: [
          { required: true, message: '请输入商品价格', trigger: 'blur' },
          { type: 'number', message: '商品价格必须是数值型', trigger: 'blur' }
        ],
        goods_number: [
          { required: true, message: '请输入商品数量', trigger: 'blur' },
          { type: 'number', message: '商品数量必须是数值型', trigger: 'blur' }
        ],
        goods_weight: [
          { required: true, message: '请输入商品重量', trigger: 'blur' },
          { type: 'number', message: '商品重量必须是数值型', trigger: 'blur' }
        ],
      },
      thisGoodId: '',
      selectKeys: [],
      goodsCateList: [],
      goodsCateProps: {
        value: 'cat_id',
        label: 'cat_name',
        childer: 'children',
      },
    }
  },
  created() {
    this.getGoodsCate()
    this.getGoodsDate()
  },
  methods: {
    getGoodsCate() {
      this.$http.get('categories').then(res => {
        this.goodsCateList = res.data.data
      })
    },
    selectGoodsChange() {
      if(this.selectKeys.length !== 3){
        // 说明选择的不是三级
        this.$message.error("请选择三级分类")
        this.selectKeys = []
      }else {
        // 是三级分类
        this.goodsForm.goods_cat = this.selectKeys.toString()
        console.log(this.selectKeys.toString())
      }
    },
    getGoodsDate() {
      this.$http.get('goods', {
        params: this.queryInfo
      }).then(res => {
        if(res.data.meta.status !== 200) {
          this.$message.error(res.data.meta.msg)
        }else {
          this.total = res.data.data.total
          this.goodsDate = res.data.data.goods
          // this.$message.success("获取商品列表成功")
        }
      })
    },
    sizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsDate()
    },
    currentChange(newNum) {
      this.queryInfo.pagenum = newNum
      this.getGoodsDate()
    },
    searchGoods() {
      this.getGoodsDate()
    },
    addGoods() {
      this.$router.push('/goods/add')
    },
    goodsEdit(scopeRow) {
      this.thisGoodId = scopeRow.goods_id
      this.goodsForm.goods_name = scopeRow.goods_name
      this.goodsForm.goods_cat = scopeRow.goods_cat
      this.goodsForm.goods_price = scopeRow.goods_price
      this.goodsForm.goods_number = scopeRow.goods_number
      this.goodsForm.goods_weight = scopeRow.goods_weight
      this.editDialogVisible = true
      console.log(this.thisGoodId)
    },
    goodsDel(scopeRow) {
      this.$confirm("此操作将永久删除该用户,是否继续?", "提示", {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        this.$http.delete(`goods/${scopeRow.goods_id}`).then(res => {
          if(res.data.meta.status !== 200){
            this.$message.error("删除失败:" + res.data.meta.msg)
          }else {
            this.$message.success("删除成功")
            this.getGoodsDate()
          }
        })
      }).catch(() => {
        this.$message.info("已取消删除")
      });
    },
    editDialogClose() {
      this.$refs.goodsFormRef.resetFields()
      this.editDialogVisible = false
    },
    editDialogSure() {
      this.$refs.goodsFormRef.validate((valid) => {
        if (valid) {
          this.$http.put(`goods/${this.thisGoodId}`, this.goodsForm).then(res => {
            if(res.data.meta.status !== 200) {
              this.$message.error(res.data.meta.msg)
            }else {
              this.$message.success("编辑(bug:重新创建--未修复)成功")
              this.getGoodsDate()
            }
          })
        } else {
          this.$message.error("请重新填写表单")
          return false;
        }
      });
    },
  },
}
</script>
<style lang="scss" scoped>
  
</style>